using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using DataAccess;
using Task.Contracts;
using NHibernate;
using NHibernate.Expression;
using System.Data;

namespace Task
{
    public class MessageTask : IMessageTask
    {

        public int GetNumberOfReceivedMessages(int UserId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            List<message> listAllMessages = sampleMessageFactory.GetByCriteria(NHibernate.Expression.Expression.Eq("to_user_id", UserId));

            return listAllMessages.Count;
        }

        public DataTable GetAllReceivedMessages(int UserId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            List<message> listAllMessages = sampleMessageFactory.GetByCriteria(NHibernate.Expression.Expression.Eq("to_user_id", UserId));


            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add("id",typeof(int));
            dt.Columns.Add("login",typeof(string));
            dt.Columns.Add("id_user", typeof(int));
            dt.Columns.Add("topic", typeof(string));
            dt.Columns.Add("date",typeof(string));
            dt.Columns.Add("status",typeof(string));

            foreach (message el in listAllMessages)
            {
                if (el.id.ToString().Length != 0)
                {
                    dr = dt.NewRow();
                    dr[0] = el.id;
                    dr[3] = el.title.ToString();
                    dr[1] = GetLoginOfUser((int)el.from_user_id);
                    dr[2] = el.from_user_id;
                    dr[4] = el.date.ToString();
                    dr[5] = el.status.ToString();
                    dt.Rows.Add(dr);
                }
            }
            return dt;

        }

        public string GetLoginOfUser(int UserId)
        {
            NHibernateDao<user, int> sampleUserFactory = new NHibernateDao<user, int>();
            user u = sampleUserFactory.GetById(UserId, false);
            return u.login;

        }

        public int GetIdOfSender(int MsgId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            message m = sampleMessageFactory.GetById(MsgId, false);

            return (int)m.from_user_id;
        }

       
        public string GetTitleOfMessage(int MessageId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            message m = sampleMessageFactory.GetById(MessageId, false);
            m.status = "y";
            sampleMessageFactory.SaveOrUpdate(m);

           
            return m.title.ToString();

        }

        public string GetStatusOfMessage(int MessageId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            message m = sampleMessageFactory.GetById(MessageId, false);

            return m.status.ToString();

        }

        public string GetContOfMessage(int MessageId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            message m = sampleMessageFactory.GetById(MessageId, false);
            m.status = "y";
            sampleMessageFactory.SaveOrUpdate(m);


            return m.cont.ToString();

        }
        

        public void AddMessage(int FromUserId,int ToUserId, string topic,string content )
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            NHibernateDao<user, int> sampleUserFactory = new NHibernateDao<user, int>();

            user u = sampleUserFactory.GetById(ToUserId,false);

            message m = new message();
            
            m.title = topic;
            m.cont = content;
            //m.from_user_id = FromUserId;
            //m.to_user_id = ToUserId;
            m.user = sampleUserFactory.GetById(FromUserId, false);
            m.user1 = sampleUserFactory.GetById(ToUserId, false);
            m.date=DateTime.Now;
            m.status="n";

            //u.Addmessage(m);
            //m.user = u;
            //sampleUserFactory.SaveOrUpdate(u);
            //sampleMessageFactory.CommitChanges();

            sampleMessageFactory.Save(m);
            //sampleMessageFactory.CommitChanges();
        }

        public void DeleteMessage(int MessageId)
        {
            NHibernateDao<message, int> sampleMessageFactory = new NHibernateDao<message, int>();
            message m=sampleMessageFactory.GetById(MessageId,false);
            sampleMessageFactory.Delete(m);

        }
    }
}
